// pages/mine/wallet/withdraw.js 
const App = getApp()
import Kits from '../../../utils/kits'
import Util from '../../../utils/util'

Page({
    data: {
        init: false,

        summ: {
            settleStr: '--',
        },
        conf: {
            flag: false,
        },

        wval: '',

        cvisb: false,

        bankOpts: ["工商银行", "农业银行", "建设银行", "中国银行", "交通银行", "招商银行", "邮储银行", "民生银行", "平安银行", "中信银行", "浦发银行", "兴业银行", "光大银行", "广发银行", "华夏银行", "宁波银行", "北京银行", "上海银行", "南京银行", "长子县融汇村镇银行", "长沙银行", "浙江泰隆商业银行", "中原银行", "企业银行（中国）", "顺德农商银行", "衡水银行", "长治银行", "大同银行", "河南省农村信用社", "宁夏黄河农村商业银行", "山西省农村信用社", "安徽省农村信用社", "甘肃省农村信用社", "天津农村商业银行", "广西壮族自治区农村信用社", "陕西省农村信用社", "深圳农村商业银行", "宁波鄞州农村商业银行", "浙江省农村信用社联合社", "江苏省农村信用社联合社", "江苏紫金农村商业银行股份有限公司", "北京中关村银行股份有限公司", "星展银行（中国）有限公司", "枣庄银行股份有限公司", "海口联合农村商业银行股份有限公司", "南洋商业银行（中国）有限公司"],
        bankIndex: null,

        auto: false,
        type: 0,
        b_card: '',
        b_name: '',
        b_bank: '',
        b_bsub: '',

        w_name: '',

        a_key: '',
        a_name: '',
    },
    onLoad() {

        this.execLoadConf()
        this.execLoadFin()
    },
    toHistory() {
        wx.navigateTo({
            url: '/pages/mine/wallet/history',
        })
    },
    doShowModal() {
        this.setData({
            cvisb: true
        })
    },
    doHideModal() {
        this.setData({
            cvisb: false
        })
    },
    doBankChange(e) {
        this.setData({
            bankIndex: e.detail.value,
            b_bank: this.data.bankOpts[parseInt(e.detail.value)]
        })
    },
    doTypeChange(e) {
        if (e.type == 'tap') {
            this.setData({
                type: parseInt(e.currentTarget.dataset.type)
            })
        } else {
            this.setData({
                type: parseInt(e.detail.value)
            })
        }
    },
    execLoadFin() {
        App.doPost({
            uri: '/app/wallet/fin/summary',
            body: {},
            fnSucc: resp => {
                resp.data.settleStr = Kits.fmtMoneyString(resp.data.settle)
                this.setData({
                    summ: resp.data
                })
            }
        })
    },
    execLoadConf() {
        App.doPost({
            uri: '/app/wallet/withdraw/config',
            body: {},
            fnSucc: resp => {
                let bidx = this.data.bankOpts.findIndex(v => v == resp.data.withdraw.bank.bank)
                this.setData({
                    init: true,
                    conf: resp.data.withdraw,

                    auto: resp.data.withdraw.auto,
                    type: resp.data.withdraw.type,
                    b_card: resp.data.withdraw.bank.card,
                    b_name: resp.data.withdraw.bank.name,
                    b_bank: resp.data.withdraw.bank.bank,
                    bankIndex: bidx >= 0 ? bidx : null,

                    w_name: resp.data.withdraw.weixin.name,

                    a_key: resp.data.withdraw.alipay.key,
                    a_name: resp.data.withdraw.alipay.name,

                })
            }
        })
    },

    execApply() {
        if (!this.data.conf.flag) {
            Util.Notify.warn(`暂时无法提现`)
            return
        }
        if (this.data.conf.type == 0) {
            Util.Notify.warn(`请设置提现方式`)
            return
        }
        if (!this.data.wval) {
            Util.Notify.warn(`请填写提现金额`)
            return
        }
        if (parseFloat(this.data.wval) < 2) {
            Util.Notify.warn(`最低提现2元`)
            return
        }
        if (parseFloat(this.data.wval) > parseFloat(this.data.summ.settleStr)) {
            Util.Notify.warn(`最大提现${this.data.summ.settleStr}元`)
            return
        }

        this.setData({
            loading: true
        })
        App.doPost({
            uri: '/app/wallet/withdraw/apply',
            body: {
                value: parseInt(parseFloat(this.data.wval) * 100)
            },
            fnSucc: () => {
                wx.pub("ReloadUserFin", {})
                Util.Toast.succ(`提交成功`)
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 1000);
            },
            fnFail: resp => {
                Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
            },
            fnComp: () => {
                this.setData({
                    loading: false,
                })
            }
        })
    },

    execSave() {

        if (this.data.type == 0) {
            Util.Notify.warn(`请选择提现方式`)
            return
        }
        if (this.data.type == 1) {
            if (!this.data.b_bank) {
                Util.Notify.warn(`请选择收款银行`)
                return
            }
            if (!this.data.b_bsub) {
                Util.Notify.warn(`请填写支行名称`)
                return
            }
            if (!this.data.b_name) {
                Util.Notify.warn(`请填写户主名称`)
                return
            }
            if (!this.data.b_card) {
                Util.Notify.warn(`请填写收款卡号`)
                return
            }
        }
        if (this.data.type == 2) {
            if (!this.data.w_name) {
                Util.Notify.warn(`请填写微信认证姓名`)
                return
            }
        }
        if (this.data.type == 3) {
            if (!this.data.a_name) {
                Util.Notify.warn(`请填写支付宝认证姓名`)
                return
            }
            if (!this.data.a_key) {
                Util.Notify.warn(`请填写支付宝收款账号`)
                return
            }
        }

        this.setData({
            loading: true
        })
        App.doPost({
            uri: '/app/wallet/withdraw/save',
            body: {
                withdraw: {
                    flag: true,
                    auto: this.data.auto || false,
                    type: this.data.type,
                    alipay: {
                        key: this.data.a_key,
                        name: this.data.a_name
                    },
                    weixin: {
                        appid: App.Global.AppId,
                        openid: App.Global.OpenId,
                        name: this.data.w_name
                    },
                    bank: {
                        bsub: this.data.b_bsub,
                        card: this.data.b_card,
                        name: this.data.b_name,
                        bank: this.data.b_bank
                    }
                }
            },
            fnSucc: () => {
                this.execLoadConf()
            },
            fnFail: resp => {
                Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
            },
            fnComp: () => {
                this.setData({
                    loading: false,
                    cvisb: false
                })
            }
        })
    }
})